package com.yuma.springshiroredisthymeleaf.config;

import org.springframework.web.servlet.LocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

public class I18nResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {  // lang=zh_CN, lang=en_US
        // 1.获取lang的参数
        String lang = request.getParameter("lang");  //zh_CN,en_US  语言_国家
        if (lang != null){
            String[] langAndCountry = lang.split("_");
            if (langAndCountry != null && langAndCountry.length == 2){
                return new Locale(langAndCountry[0], langAndCountry[1]);
            }
        }
        // 2.如果没有lang参数则返回默认的本地化
        return Locale.getDefault();
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    }
}
